From 31d151c9483c174f0149d20868f9100e4032f5cf Mon Sep 17 00:00:00 2001 From: oliskoli Date: Sun, 26 Mar 2006 21:58:12 +0000 Subject: [PATCH] Let work xstrndup and xstrndupt with non-terminated strings. --- gpsbabel/util.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/gpsbabel/util.c b/gpsbabel/util.c index 82d61a266..9d90b1066 100644 --- a/gpsbabel/util.c +++ b/gpsbabel/util.c @@ -144,14 +144,15 @@ XSTRNDUP(const char *str, size_t sz, DEBUG_PARAMS ) xstrndup(const char *str, size_t sz) #endif { - size_t newlen; + size_t newlen = 0; + char *cin = (char *)str; char *newstr; - newlen = strlen(str); - if (newlen > sz) { - newlen = sz; + while ((newlen < sz) && (*cin != '\0')) { + newlen++; + cin++; } - + newstr = (char *) xmalloc(newlen + 1); memcpy(newstr, str, newlen); newstr[newlen] = 0; @@ -170,17 +171,18 @@ XSTRNDUPT(const char *str, size_t sz, DEBUG_PARAMS ) xstrndupt(const char *str, size_t sz) #endif { - size_t newlen; + size_t newlen = 0; + char *cin = (char *)str; char *newstr; - newlen = strlen(str); - if (newlen > sz) { - newlen = sz; + while ((newlen < sz) && (*cin != '\0')) { + newlen++; + cin++; } - + newstr = (char *) xmalloc(newlen + 1); - memcpy(newstr, str, newlen); - newstr[newlen] = '\0'; + memcpy(newstr, str, newlen); + newstr[newlen] = 0; rtrim(newstr); return newstr; -- 2.30.2